{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本质：统计每个像素灰度 出现的概率 0-255 p\n",
    "# 累计概率 \n",
    "# 1 0.2  0.2\n",
    "# 2 0.3  0.5\n",
    "# 3 0.1  0.6\n",
    "# 256 \n",
    "# 100 0.5 255*0.5 = new \n",
    "# 1 统计每个颜色出现的概率 2 累计概率 1 3 0-255 255*p\n",
    "# 4 pixel \n",
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "cv2.imshow('src',img)\n",
    "\n",
    "imgInfo = img.shape\n",
    "height = imgInfo[0]\n",
    "width = imgInfo[1]\n",
    "\n",
    "count_b = np.zeros(256,np.float)\n",
    "count_g = np.zeros(256,np.float)\n",
    "count_r = np.zeros(256,np.float)\n",
    "for i in range(0,height):\n",
    "    for j in range(0,width):\n",
    "        (b,g,r) = img[i,j]\n",
    "        index_b = int(b)\n",
    "        index_g = int(g)\n",
    "        index_r = int(r)\n",
    "        count_b[index_b] = count_b[index_b]+1\n",
    "        count_g[index_g] = count_g[index_g]+1\n",
    "        count_r[index_r] = count_r[index_r]+1\n",
    "for i in range(0,255):\n",
    "    count_b[i] = count_b[i]/(height*width)\n",
    "    count_g[i] = count_g[i]/(height*width)\n",
    "    count_r[i] = count_r[i]/(height*width)\n",
    "#计算累计概率\n",
    "sum_b = float(0)\n",
    "sum_g = float(0)\n",
    "sum_r = float(0)\n",
    "for i in range(0,256):\n",
    "    sum_b = sum_b+count_b[i]\n",
    "    sum_g = sum_g+count_g[i]\n",
    "    sum_r = sum_r+count_r[i]\n",
    "    count_b[i] = sum_b\n",
    "    count_g[i] = sum_g\n",
    "    count_r[i] = sum_r\n",
    "#print(count)\n",
    "# 计算映射表\n",
    "map_b = np.zeros(256,np.uint16)\n",
    "map_g = np.zeros(256,np.uint16)\n",
    "map_r = np.zeros(256,np.uint16)\n",
    "for i in range(0,256):\n",
    "    map_b[i] = np.uint16(count_b[i]*255)\n",
    "    map_g[i] = np.uint16(count_g[i]*255)\n",
    "    map_r[i] = np.uint16(count_r[i]*255)\n",
    "# 映射\n",
    "dst = np.zeros((height,width,3),np.uint8)\n",
    "for i in range(0,height):\n",
    "    for j in range(0,width):\n",
    "        (b,g,r) = img[i,j]\n",
    "        b = map_b[b]\n",
    "        g = map_g[g]\n",
    "        r = map_r[r]\n",
    "        dst[i,j] = (b,g,r)\n",
    "cv2.imshow('dst',dst)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
